package view.components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.BorderFactory;

import view.GedivaGUI;

/**
 * A labeled input field that allows the user to input a string.
 * 
 * @author Vijay Ram
 */

@SuppressWarnings("serial")
public class FileInputBoxArea extends JPanel
{
    private JTextField myInput;

    /**
     * Create input that gets data from the db and displays it in the canvas.
     * 
     * @param prompt
     * @param db
     * @param target
     */
    // public LabeledInput(Properties constants, String prompt,
    // final NameDatabase db, final Canvas target) {
    public FileInputBoxArea(final Properties constants,
            final GedivaGUI gedivaGUI)
    {

        super(new BorderLayout());
        setBorder(BorderFactory.createEtchedBorder());

        // Add components
        JLabel label = new JLabel(constants.getProperty("INPUT_PROMPT"));
        JButton process = new JButton(constants
                .getProperty("FILE_PROCESS_BUTTON"));
        JButton open = new JButton(constants.getProperty("OPEN_BUTTON"));
        JButton url = new JButton(constants.getProperty("URL_BUTTON"));
        JButton save = new JButton(constants.getProperty("SAVE_BUTTON"));
        JButton visible = new JButton(constants.getProperty("PANEL_VISIBLE"));
        myInput = new JTextField(40);
        myInput.setPreferredSize(new Dimension(40, 30));

        add(label, BorderLayout.WEST);
        add(myInput, BorderLayout.EAST);

        Box buttons = Box.createHorizontalBox();
        buttons.add(process);
        buttons.add(url);
        buttons.add(open);
        buttons.add(save);
        buttons.add(visible);

        add(buttons, BorderLayout.SOUTH);

        // Set Action Listeners
        myInput.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ev)
            {
                try
                {
                    gedivaGUI.processFile((new File(myInput.getText()).toURI()
                            .toURL()));
                }
                catch (MalformedURLException e)
                {
                    gedivaGUI.showMessage(e.getMessage());
                }
                gedivaGUI.showMessage(myInput.getText());
            }
        });

        // Set Action Listeners
        url.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ev)
            {
                try
                {
                    gedivaGUI.processFile(new URL(myInput.getText()));
                }
                catch (MalformedURLException e)
                {
                    gedivaGUI.showMessage(e.getMessage());
                }
                gedivaGUI.showMessage(myInput.getText());
            }
        });

        process.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ev)
            {
                try
                {
                    gedivaGUI.processFile(new URL(myInput.getText()));
                }
                catch (MalformedURLException e)
                {
                    gedivaGUI.showMessage(e.getMessage());
                }
                gedivaGUI.showMessage(myInput.getText());
            }
        });

        open.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ev)
            {
                final JFileChooser open = new JFileChooser(new File(constants
                        .getProperty("DEFAULT_DATA_DIRECTORY")));

                // add action listener
                open.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent ev)
                    {
                        try
                        {
                            gedivaGUI.processFile(open.getSelectedFile()
                                    .toURI().toURL());
                        }
                        catch (MalformedURLException e)
                        {
                            gedivaGUI.showMessage(e.getMessage());
                        }
                    }
                });

                // allow open file to be clicked
                open.showDialog(null, "Process Data File");
            }
        });

        save.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ev)
            {
                final JFileChooser save = new JFileChooser(new File(constants
                        .getProperty("DEFAULT_DATA_DIRECTORY")));

                // add action listener
                save.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent ev)
                    {
                        gedivaGUI.showMessage("Saving.... 99% Complete");
                    }
                });

                // allow open file to be clicked
                save.showDialog(null, "Save Graph");
            }
        });

        visible.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ev)
            {
                gedivaGUI.togglePanelVisibility();
            }
        });

    }

    public String getText()
    {
        return myInput.getText();
    }
}
